public class MySingleList implements IList{

    // 节点的内部类
    static class ListNode{
        public int val;
        public ListNode next;
        // next默认不知道下一个节点的地址,默认为 null

        // 构造方法没有返回值
        public ListNode(int val){
            this.val = val;
        }
    }

    public ListNode head;

    public ListNode createList(){
        ListNode node1 = new ListNode(12);
        ListNode node2 = new ListNode(13);
        ListNode node3 = new ListNode(14);
        ListNode node4 = new ListNode(15);
        ListNode node5 = new ListNode(16);

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;

        head = node1;

        return head;
    }

    @Override
    public void addFirst(int data) {
        ListNode node = new ListNode(data);

        if(this.head == null){
            this.head = node;
        }else{
            node.next = this.head;
            this.head = node;
        }
    }

    @Override
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        ListNode cur = head;

        if(this.head == null){
            this.head = node;
        }
        else{
            while(cur.next != null){
                cur = cur.next;
            }
            cur.next = node;
        }
    }

    @Override
    public void addIndex(int index, int data) {
        ListNode node = new ListNode(data);
        if(index < 0 || index > size()){
           // throw new IndexOutOfBoundsException("下标出现异常" + " Index:" + index);
            return ;
        }

        if(index == 0){
            addFirst(data);
        }
        else if(index == size()){
            addLast(data);
        }
        else{
            // 找到index - 1位置的节点
            ListNode cur = searchPrev(index);
            node.next = cur.next;
            cur.next = node;
        }
    }

    public ListNode searchPrev(int index){
        ListNode cur = this.head;
        int count = 0;

        while(count != index - 1){
            cur = cur.next;
            count++;
        }

        return cur;
    }

    @Override
    public boolean contains(int key) {
        ListNode cur = head;
        while(cur != null){
            if(cur.val == key){
                return true;
            }
            cur = cur.next;
        }

        return false;
    }

    @Override
    public void remove(int key) {
        if(this.head == null){
            System.out.println("一个节点都没有");
            return;
        }

        // 头结点为要删除的节点
        if(this.head.val == key){
            ListNode cur = this.head.next;
            this.head = cur;
            return;
        }

        ListNode prev = findPrev(key);

        if(prev == null){
            // 没有值为key的节点
            System.out.println("没有值为key的节点");
            return;
        }
        else{
            ListNode del = prev.next;
            prev.next = del.next;
        }
    }

    /*
    找到值为key的前驱节点
     */
    public ListNode findPrev(int key){
        ListNode cur = this.head;

        while(cur.next != null){
            if(cur.next.val == key){
                return cur;
            }
            cur = cur.next;
        }

        return null;
    }

    /*
    删除所有值为 key 的节点
     */
    @Override
    public void removeAllKey(int key) {
         if(this.head == null){
             // 没有节点可以删除
             return;
         }

         ListNode cur = this.head.next;
         ListNode prev = this.head;

         // 除头结点外是val的节点要删除
         while(cur != null){
             if(cur.val == key){
                 prev.next = cur.next;
                 cur = cur.next;
             }
             else{
                 prev = cur;
                 cur = cur.next;
             }
         }

         // 如果头结点是val要删除
         if(this.head.val == key){
             this.head = this.head.next;
         }
    }

    @Override
    public int size() {
        int count = 0;
        ListNode cur = this.head;

        while(cur != null) {
            cur = cur.next;
            count++;
        }

        return count;
    }

    @Override
    public void clear() {
        if(this.head == null){
            return;
        }

        ListNode cur = this.head;
        while(cur != null){
            ListNode ccur = cur.next;
            cur.next = null;
            // cur.val = 0;
            cur = ccur;
        }
        // 记得释放头节点
        head = null;
    }

    @Override
    public void display() {
        ListNode cur = this.head;
        while(cur != null){
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    public ListNode findKthToTail(int k){
        /*if(k <= 0 || k > size()){
            return null;
        }*/

        if(k <= 0 || head == null){
            return null;
        }


        ListNode slow = head;
        ListNode fast = head;
        int count = k - 1;

        while(count != 0){
            fast = fast.next;
            // 处理k太大的问题 
            if(fast == null){
                return null;
            }
            count--;
        }

        while(fast.next != null){
            slow = slow.next;
            fast = fast.next;
        }

        return slow;
    }

}
